package com.dss.auto_deploy.quartz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dss.auto_deploy.quartz.domain.SysJobLog;
import com.dss.auto_deploy.quartz.dto.SysJobLogPageDto;
import com.dss.auto_deploy.quartz.mapper.SysJobLogMapper;
import com.dss.auto_deploy.quartz.service.ISysJobLogService;
import com.dss.auto_deploy.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author lenovo
 */
@Service
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class SysJobLogServiceImpl extends ServiceImpl<SysJobLogMapper, SysJobLog> implements ISysJobLogService {
    private final SysJobLogMapper jobLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param pageDto 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public Page<SysJobLog> selectJobLogList(SysJobLogPageDto pageDto) {
        Page<SysJobLog> tablesPage = new Page<>(pageDto.getPage(), pageDto.getLimit());
        LambdaQueryWrapper<SysJobLog> queryWrapper = Wrappers.lambdaQuery(SysJobLog.class);
        if (StringUtils.isNotEmpty(pageDto.getJobName())){
            queryWrapper.like(SysJobLog::getJobName,pageDto.getJobName());
        }
        if (StringUtils.isNotEmpty(pageDto.getInvokeTarget())){
            queryWrapper.like(SysJobLog::getInvokeTarget,pageDto.getInvokeTarget());
        }
        if (StringUtils.isNotEmpty(pageDto.getJobGroup())){
            queryWrapper.eq(SysJobLog::getJobGroup,pageDto.getJobGroup());
        }
        if (StringUtils.isNotEmpty(pageDto.getStatus())){
            queryWrapper.eq(SysJobLog::getStatus,pageDto.getStatus());
        }
        if (StringUtils.isNotEmpty(pageDto.getBeginTime())){
            queryWrapper.ge(SysJobLog::getCreateTime,pageDto.getBeginTime());
        }
        if (StringUtils.isNotEmpty(pageDto.getBeginTime())){
            queryWrapper.le(SysJobLog::getCreateTime,pageDto.getEndTime());
        }
        queryWrapper.orderByDesc(SysJobLog::getCreateTime);
        Page<SysJobLog> pageSelectResult = jobLogMapper.selectPage(tablesPage, queryWrapper);
        return pageSelectResult;
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return jobLogMapper.selectJobLogById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        jobLogMapper.insert(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        return jobLogMapper.deleteJobLogByIds(logIds);
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId) {
        return jobLogMapper.deleteJobLogById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        jobLogMapper.cleanJobLog();
    }
}
